<?php
global $conf;
$post=$_POST;
$table='user';
$id='user_id';
/*
	TODO add 404 and error messages         
*/
$post=process_serialized_data($post,'serial','user_serial');
$catch="user_email";
$conf->style['masthead']='Profile';
//$post['user_serial']=$post['content_serial'];

if($conf->module=='user')
{
	$signed_up=false;
	$activated=false;
	$user_process=new user;
	if($conf->key=='signup')
	{
		
		mp("Signup");
		
		if(!empty($post[$catch]))
		{
			mp("process");
			
			$post['user_password']=md5($post['user_password']); 
			$user_process->post=$post;
			$code=$user_process->add_user();
			
			$signed_up=true;
		}
	}
	else if($conf->key=='profile')
	{	
		$catch="serial_first_name";
		
		mp("Modify Profile");
		$user_process->user_id=$_SESSION['user_id'];
	
		if( !empty($post[$catch]))
		{
				$conf->messages[]='Profile Modified';
				process_sql_post($post,$table,'update',$id,$_SESSION['user_id']);
		
		}
		
			$e=$user_process->get_user_info();
	}
	
	
	else if($conf->key=='activate')
	{	
		//mp($conf->cat);
		if(!empty($conf->cat))
		{
			$user_process->user_confirm_code=$conf->cat;
			$processed=$user_process->activate_user();

			mp("Cat Active :".$conf->cat);
			$activated=true;
		}
		else
		{
			$activated=false;
		}
	}
	else if($conf->key=='login')
	{

	}
	else if($conf->key=='password')
	{
		if($post['change_password'] == 'a')
		{
			mp("send code");
			$user_process->post=$post;
			$code_sent=$user_process->change_password();
			
		}
		else if($post['password_entered'] == 'a' && !empty($_POST['user_password_code']))
		{
			$post['user_password']=md5($post['user_password']); 
			$user_process->post=$post;
			$password_replaced=$user_process->update_password();
			mp('password entered, updating');
		
		//	$password_replaced=$user_process->update_password();
		}
		else if(!empty($conf->cat))
		{
			$user_process->user_password_code=$conf->cat;
			mp('show password box');
			$password_box=$user_process->validate_password_code();
		}

	}
	else if($conf->key=='fbsignup' && !empty($_POST['user_fb_uid']))
	{
		mp("process");
		$post['serial_first_name']=$auth_session->fb['me']['first_name'];
		$post['serial_last_name']=$auth_session->fb['me']['last_name'];
		$post['user_email']=$auth_session->fb['me']['email'];
		$post['user_fb_uid']=$auth_session->fb['me']['id'];
		$post['user_password']=md5("FBUSER"); 
		$post['user_confirm']=1;
		//$user_process->post=$post;
		//$code=$user_process->add_user();
		$signed_up=true;
	//	header("location: ".$conf->absolute_path);
	}
}



if($conf->module=='user')
{
	mp('loaded user '.$conf->qm[1]);
	if ($conf->qm[1]=='secret')
		{
				$user_process->user_id=$_SESSION['user_id'];
				$e=$user_process->get_user_info();
		//		print_r($_SESSION['user_id']);
			$conf->style['masthead'].=': Verification';
				mp('loaded secret');
				
				if(!empty($_POST))
				{
				
					
			
					if($_POST['ask']=='logout')
					{
							mp('logging out');
							$_SESSION['secret']='';
							$auth_session->logout();
							$conf->messages[]='You have been Logged Out';

							$conf->set_default_q();
					}
					else
					{
						mp('verifying');
						rp($_POST);
						$auth_session->answer=$_POST['user_answer'];
						if($auth_session->verify_secret())
						{
								$conf->messages[]='You are now Verified, Enjoy the Show';
								$conf->set_default_q();
						}
						
						else
						{
								$conf->errors[]='Invalid Answer';
								$conf->page_render= 'admin/m/user/b_secret.php';
						}
						
					
					}
				}
				else
				{
					$conf->page_render= 'admin/m/user/b_secret.php';
				}
				
				
				
		}
	else if($conf->key=='signup')
	{
		if($signed_up)
		{
			$conf->page_render= 'admin/m/user/b_signed_up.php';
		}
		else
		{
			$conf->page_render= 'admin/m/user/b_signup.php';
		}
	}
	else if($conf->key=='fbsignup')
	{
		if($signed_up==true)
		{
		
		}
		else
		{
			$post['serial_first_name']=$auth_session->fb['me']['first_name'];
			$post['serial_last_name']=$auth_session->fb['me']['last_name'];
			$post['user_email']=$auth_session->fb['me']['email'];
			$post['user_fb_uid']=$auth_session->fb['me']['id'];
			$post['user_password']=md5("FBUSER"); 
			$post['user_confirm']=1;
		
			$user_process->post=$post;
			$code=$user_process->add_user();
			$signed_up=true;
			header("location: ".$conf->absolute_path);
			//$_SESSION['whereto']=$_SESSION['page_referrer'];
			//$conf->page_render= 'admin/m/user/b_fb_signup.php';
		}
			
		
	}
	else if ($conf->key=='activate')
	{
		if($activated)
		{
					$conf->messages[]='Your Account is now Active';
			$conf->page_render= 'admin/m/user/b_activate.php';
		}
		else
		{
					$conf->messages[]='Activation Error';

		}
	}
	else if ($conf->key=='login')
	{
	//	global $login_error;
		if($auth_session->login_error)
		{

			$conf->page_render= 'admin/m/user/b_login.php';
		}
		else if($auth_session->log_in_success)
		{
			$conf->module='content';
			$conf->key_string='home';
			$conf->qm[0]='watch';
			$conf->qm[1]=2;
		
			$conf->page_render='assets/pages/home.php';
		}
		else
		{

			$conf->page_render= 'admin/m/user/b_login.php';
		}


	}
	else if ($conf->key=='logout')
	{
			$conf->set_default_q();
			global $auth_session;
		
			mp('qm: '.$conf->cat);
	
			if($conf->cat=='duplicate')
			{
				$conf->errors[]='Logged out';
						$auth_session->logout_duplicate($conf->key_2);
			}
			else
			{
						$auth_session->logout();
			}
			
			$conf->messages[]='You have been Logged Out';
		
			
		
			

	}
	else if ($conf->key=='profile')
	{
		if($auth_session->logged_in)
		{
		$conf->page_render= 'admin/m/user/b_profile.php';

			/*
				TODO fix user update
			*/
		}
		else
		{
	
				$conf->page_render= 'admin/m/user/b_login.php';

		}
	}

	else if ($conf->key=='password')
	{
		if($auth_session->logged_in)
		{
		$conf->page_render= 'admin/m/user/b_profile.php';
		}

		else
		{
			if($code_sent)
			{
				/*
					TODO put messages for password change
				*/
				$conf->set_default_q();
				mp('Code Sent');
					$conf->messages[]='Code Sent to your Email';
			}
			else if($password_replaced)
			{
				$conf->set_default_q();
				mp('Password Replaced');
					$conf->messages[]='Password Replaced';
			}
			else if($password_box)
			{
	
				$conf->page_render= 'admin/m/user/b_password_box.php';
				$conf->style['masthead'].=' : Password Reset';
				//$crumbs
			}
			else
			{
				$conf->page_render= 'admin/m/user/b_password.php';
				$conf->style['masthead'].=' : Password Reset';
			}
	

		}

	}

	else
	{
		$conf->page_render= '404.php';
	}

}
mp($conf->page_render);

?>